package com.grm.datasource.service;

import com.grm.datasource.config.DatasourceScope;
import com.grm.datasource.entity.User;
import com.grm.datasource.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;

    @DatasourceScope(scope = "slave1")
    public void add(User user){
        userMapper.save(user);
    }

    @DatasourceScope(scope = "slave2")
    public void update(User user){
        userMapper.update(user);
    }

    @DatasourceScope(scope = "slave1")
    public void delete(Long id){
        userMapper.deleteById(id);
    }

    @DatasourceScope(scope = "slave2")
    public User getById(Long id){
        return userMapper.selectById(id);
    }

    // 不指定，使用主数据源
    // @DatasourceScope(scope = "slave1")
    public List<User> getList(){
        return userMapper.selectAll();
    }
}
